package com.atguigu.java;

import org.junit.Test;

import java.io.*;

public class FileIOPutTest {
    public void copyFile(String srcPath,String desPath) throws IOException {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            File f1 = new File(srcPath);
            File f2 = new File(desPath);
            fis = new FileInputStream(f1);
            fos = new FileOutputStream(f2);
            byte[] bytes = new byte[1024];
            int len;
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    @Test
    public void copyFile() throws IOException {
        long start = System.currentTimeMillis();
        String srcPath="hello.txt";
        String desPath="hello3.txt";
        copyFile(srcPath,desPath);
        long end= System.currentTimeMillis();
        System.out.println("花费的时间为:"+(end-start));
    }
    @Test
    public void test() throws IOException {
        FileInputStream fis=null;
        try {
            File file = new File("hello.txt");
             fis= new FileInputStream(file);
            byte[] buffer = new byte[5];
            int len;
            while ((len=fis.read(buffer))!=-1) {
                String str = new String(buffer, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void test2() throws IOException {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            File f1 = new File("1.jpg");
            File f2 = new File("2.jpg");
            fis = new FileInputStream(f1);
             fos = new FileOutputStream(f2);
            byte[] bytes = new byte[5];
            int len;
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
            System.out.println("复制成功");
        } finally {
            if(fos!=null)
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if(fis!=null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }
}
